$(function () {
    var ctx=$("#ctxVal").val();
   $("#btn_submit").click(function () {
       validation();
   });
    $("#btn_resetting").click(function () {
        resetting();
    });
    $("#btn_cancel").click(function () {
        msgHidden();
    });
    $("#btn_save").click(function () {
        formSubmit(ctx);
    });
    $("#btn_return").click(function () {
        returns(ctx);
    });

    Validators();
});
/*验证*/
function validation() {
    $("#saveForm").bootstrapValidator('validate');
    var flag =  $("#saveForm").data('bootstrapValidator').isValid();
    if(flag){
        msgShow("确定提交信息吗?");
    }
}
/*打开提示*/
function msgShow(msg) {
    $("#confirm-body").text(msg);
    $("#confirmComeOn").modal("show")
}
/*关闭提示*/
function msgHidden(msg) {
    $("#confirm-body").text("");
    $('#confirmComeOn').modal('hide');
}
/*提交*/
function formSubmit(ctx){
    var formData = $("#saveForm").serialize();
    $.ajax({
        type: 'post',
        url:ctx+"/consumer.action?method=save&_="+Math.random(),
        dataType: 'json',
        data:formData,
        success: function(data){
            if(data.msg=="success"){
                layer.msg("添加用户成功!",{icon:6,time:1000*2});
                resetting();
            }else if(data.msg=="fail"){
                layer.msg("添加用户失败，请核对信息稍后再试。",{icon:7,time:1000*2});
            }else{
                layer.alert("再添加用户过程中遇到一个未知错误，请联系管理员进行处理！",{icon:5});
            }
        },
        error: function(xhr, type){
        }
    });
}

function resetting(){
    $("#username").val("");
    $("#name").val("");
    $("#phone").val("");
    $("#address").val("");
    $("#password").val("");

}
/*验证规则*/
function Validators() {
    $("#saveForm").bootstrapValidator({
        message: '验证失败',
        live:'enabled',
        submitButtons:'#btn_save',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                message: '用户名验证失败',
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                    stringLength: {
                        min: 4,
                        max: 18,
                        message: '用户名长度必须在4到18位之间'
                    },
                }
            },
            phone: {
                message: '手机验证失败',
                validators: {
                    notEmpty: {
                        message: '手机号不能为空'
                    },
                    stringLength: {
                        min: 11,
                        max: 11,
                        message: '手机号位数错误'
                    },
                    regexp: {
                        regexp: /^1[3|4|5|7|8][0-9]\d{4,8}$/,
                        message: '手机号格式错误'
                    }
                },
                password: {
                    message: '密码验证失败',
                    validators: {
                        notEmpty: {
                            message: '密码不能为空！'
                        },
                    }
                },
                address: {
                    message: '地址验证失败',
                    validators: {
                        notEmpty: {
                            message: '地址不能为空'
                        }
                    }
                },
            },
        }
    });

}
function  returns(ctx) {
    window.location.href=ctx+"/consumer.action";
}